package cn.campus.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;


import cn.campus.pojo.Forum;
import cn.campus.pojo.ForumAndUser;
import cn.campus.pojo.User;
import cn.campus.service.ForumService;
import cn.campus.service.UserService;

@Controller
@RequestMapping("/forum")
public class ForumController {
	@Resource
	private ForumService forumService;
	@Resource
	private UserService userService;

	@RequestMapping("addForum")
	@ResponseBody
	public String addForum(Forum forum, HttpSession httpSession) {
		User user = (User) httpSession.getAttribute("user");
		if (user == null) {
			return JSON.toJSONString("用户未登录");
		}
		String forum_id = UUID.randomUUID().toString();
		forum.setForum_id(forum_id);
		forum.setUser_id(user.getUser_id());
		// forum_content过去庞大，应生成文件保存
		String realPath = httpSession.getServletContext().getRealPath(
				"/WEB-INF/forum");
		File storeDirectory=new File(realPath);
		if(!storeDirectory.exists()){
			storeDirectory.mkdirs();
		}
		String childDirectory = makeChildDirectory(storeDirectory);
		String uuid = UUID.randomUUID().toString()+".txt";
		File file=new File(storeDirectory,childDirectory+File.separator+uuid);
		try {
			FileOutputStream fos=new FileOutputStream(file);
			byte[] bs = forum.getForum_content().getBytes();
			try {
				fos.write(bs);
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//forum_content在数据库上保存的是forum内容文件的地址
		forum.setForum_content(storeDirectory+File.separator+childDirectory+File.separator+uuid);
		int i = forumService.addForum(forum);
		if (i >= 0) {
			return JSON.toJSONString("发表帖子成功");
		}else{
			return JSON.toJSONString("发表帖子失败");
		}

	}

	private String makeChildDirectory(File storeDirectory) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateDirectory = sdf.format(new Date()); // 只管创建目录 File file = new
		File file=new File(storeDirectory, dateDirectory);
		if (!file.exists()) {
			file.mkdirs();
		}
		return dateDirectory;
	}

	@RequestMapping("/findAllForums")
	@ResponseBody
	public String findAllForums(HttpServletRequest httpServletRequest){
		String currentPage = httpServletRequest.getParameter("currentPage");
		String count = httpServletRequest.getParameter("count");
		String hot_dot = httpServletRequest.getParameter("hot_dot");
		ForumAndUser forumAndUser=new ForumAndUser();
		forumAndUser.setCurrentPage(Integer.parseInt(currentPage));
		forumAndUser.setCount(Integer.parseInt(count));
		forumAndUser.setHot_dot(hot_dot);
		ForumAndUser findAllForums = forumService.findAllForums(forumAndUser);
		return JSON.toJSONString(findAllForums);
	}
	@RequestMapping("/findForum")
	@ResponseBody
	public String findForum(HttpServletRequest httpServletRequest){
		String forum_id = httpServletRequest.getParameter("forum_id");
		//找到此贴文
		Forum forum=forumService.findForum(forum_id);
		String res=null;
		try {
			//从服务器上取出贴文的内容
			FileInputStream fis=new FileInputStream(forum.getForum_content());
			try {
				byte[] buffer=new byte[fis.available()];
				fis.read(buffer);
				res=new String(buffer);
				forum.setForum_content(res);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//找到此贴文的作者
		User user = userService.findUser(forum.getUser_id());
		//返回
		ForumAndUser forumAndUser=new ForumAndUser();
		List<Forum> forums=new ArrayList<Forum>();
		forums.add(forum);
		List<User> users=new ArrayList<User>();
		users.add(user);
		forumAndUser.setForums(forums);
		forumAndUser.setUsers(users);
		return JSON.toJSONString(forumAndUser);
	}
}
